package com.example.testproj.aidl;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.testproj.R;

import java.util.List;

public class AidlTestActivity extends AppCompatActivity {
    private AidlTestInterface aidlTestInterface;
    private MyAidlTestInterface myAidlTestInterface;
    private int index = 0;
    private static final String TAG = "AidlTestActivity";
    private IBinder.DeathRecipient mDeathRecipient=new IBinder.DeathRecipient() {
        @Override
        public void binderDied() {
            if (aidlTestInterface==null){
                return;
            }
            aidlTestInterface.asBinder().unlinkToDeath(mDeathRecipient,0);
            aidlTestInterface=null;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_aidl_test);

        bindService(new Intent(this, AidlTestService.class), connection, BIND_AUTO_CREATE);
        bindService(new Intent(this, MyAidlTestService.class), myConnection, BIND_AUTO_CREATE);

    }

    private ServiceConnection myConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            Log.d(TAG, "onServiceConnected: ");
            myAidlTestInterface = MyAidlTestStub.asInterface(iBinder);
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Log.d(TAG, "onServiceDisconnected: ");
        }
    };

    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            Log.d(TAG, "onServiceConnected: ");
            aidlTestInterface = AidlTestInterface.Stub.asInterface(iBinder);
            try {
                iBinder.linkToDeath(mDeathRecipient,0);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Log.d(TAG, "onServiceDisconnected: ");
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public void onclick(View view) {
        List<Book> bookList;
        try {
            switch (view.getId()) {
                case R.id.button3:
                    index++;
                    aidlTestInterface.addBook(new Book("书籍" + index, index, "作者" + index));
                    break;

                case R.id.button4:
                    bookList = aidlTestInterface.getBookList();
                    Toast.makeText(AidlTestActivity.this.getBaseContext(), "现在有书籍" + bookList, Toast.LENGTH_SHORT).show();
                    for (Book book : bookList) {
                        Log.d(TAG, "onclick: "+book.toString());
                    }
                    break;
                case R.id.btn_add_mybook:
                    index++;
                    myAidlTestInterface.addBook(new Book("书籍" + index, index, "作者" + index));
                    break;
                case R.id.btn_get_mybook_list:
                    bookList = myAidlTestInterface.getBookList();
                    Toast.makeText(AidlTestActivity.this.getBaseContext(), "现在有书籍" + bookList, Toast.LENGTH_SHORT).show();
                    for (Book book : bookList) {
                        Log.d(TAG, "onclick: "+book.toString());
                    }
                    break;

            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
